/*
 * Copyright (c) 2011. c.mueller
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * The Software shall be used for Good, not Evil.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package de.altitude.cimuelle.db;

import de.altitudecustomcommands.Player;
import org.apache.log4j.Logger;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * User: cybernaut
 * Date: 28.09.11
 * Time: 19:55
 */
public class DatabaseGoals {
    private static final Logger LOGGER = Logger.getLogger("DatabaseGoals");
    private transient final Connection connection = new DatabaseConnectionCreator().getCon();


    private final static String INSERT = "INSERT INTO `altitude_goals` (" +
            "`id` ," +
            "`port` ," +
            "`server_time` ," +
            "`s_name` ," +
            "`s_vapor`, " +
            "`assist`, " +
            "`timestamp`" +
            ")" +
            "VALUES (NULL,?,?,?,?,?,NOW())";
    private transient final String port;
    private transient final long server_time;
    private transient final Player source;
    private transient final boolean assist;

    public DatabaseGoals(final String port, final Number server_time, final Player source, final boolean assist) {
        this.port = port;
        this.server_time = server_time.longValue();
        this.source = source;
        this.assist = assist;
        writeLogLineIntoDB();
    }

    private void writeLogLineIntoDB() {
        synchronized (connection) {
            PreparedStatement preparedStatement = null;
            try {
                preparedStatement = connection.prepareStatement(INSERT);
                preparedStatement.setString(1, port);
                preparedStatement.setLong(2, server_time);
                preparedStatement.setString(3, source.getName());
                preparedStatement.setString(4, source.getVapor());
                preparedStatement.setString(5, (assist) ? "1" : "0");
                LOGGER.debug(preparedStatement);
                preparedStatement.executeUpdate();
                LOGGER.debug(port + ":Line written to DB.");
            } catch (SQLException e) {
                LOGGER.error(e, e);
            } finally {
                if (preparedStatement != null) {
                    try {
                        preparedStatement.close();
                    } catch (SQLException e) {
                        LOGGER.debug(e);
                    }
                }
            }
        }
    }
}
